# GCC, the GNU Compiler Collection
CC := gcc
CXX := g++
LD := g++
AS := gas
AR := ar
RM := rm
RANLIB := ranlib
CFLAGS := -c -g -D__OBJ_X64
LDFLAGS :=
LIB_DIR := libs
INCLUDES := -Isrc/core/inc -Isrc/platforms/interfaces/osal/inc -Isrc/utils/common/inc -Isrc/platforms/posix/osal/inc -Isrc/epa/inc -Isrc/utils/expr -Isrc/utils/oop -Isrc/ipc/ipc-framework/inc -Isrc/ipc/ipc-services/sim/inc -Isrc/ipc/ipc-services/core/inc -Isrc/ipc/ipc-client/inc -Itools/protobuf/include
PROTOC ?= protoc

MSG_HANDLER_SOURCES  = src/core/src/Handler.c \
    src/core/src/Message.c \
    src/core/src/MessageQueue.c \
    src/core/src/Looper.c \
    src/core/src/AsyncResult.c \
    src/core/src/Registrant.c \
    src/core/src/MessagePool.c \
    src/core/src/ArrayList.c \
    src/core/src/RegistrantList.c \
    src/core/src/Bundle.c \
    src/epa/src/Event.c \
    src/epa/src/Action.c \
    src/epa/src/PolicyService.c \
    src/platforms/posix/osal/src/Lock.c \
    src/platforms/posix/osal/src/Signal.c \
    src/platforms/posix/osal/src/ThreadLocal.c \
    src/platforms/posix/osal/src/OSMemory.c \
    src/ipc/ipc-framework/src/IpcChannel.c \
    src/ipc/ipc-framework/src/IpcChannelManager.c \
    src/ipc/ipc-framework/src/TransactionList.c \
    src/ipc/ipc-framework/src/SocketServerChannel.c \
    src/ipc/ipc-framework/src/SocketClientChannel.c \

MSG_HANDLER_CPP_SOURCES  = src/platforms/posix/osal/src/Atomic.cpp

IPC_SERVER_SOURCES = \
    src/ipc/ipc-services/sim/src/IpcSimService.c \
    src/ipc/ipc-services/sim/src/IpcSimService.pb-c.c \
    src/ipc/ipc-services/core/src/IpcMgrService.c \
    src/ipc/ipc-services/core/src/IpcServiceManager.c \
    src/ipc/ipc-services/core/src/IpcServer.c \
    src/ipc/ipc-services/core/src/IpcService.c \

IPC_CLIENT_SOURCES = \
    src/ipc/ipc-client/src/IpcClientManager.cpp \
    src/ipc/ipc-client/src/IpcClientLoader.cpp \
    src/ipc/ipc-client/src/IpcClient.cpp \
    src/ipc/ipc-client/src/IpcSimClient.cpp \
    src/ipc/ipc-client/src/IpcApi.cpp \

IPC_CLIENT_GEN_SOURCES = \
    src/ipc/ipc-client/src/IpcSimService.pb.cc \


MSG_HANDLER_OBJECTS = $(MSG_HANDLER_SOURCES:.c=.o) $(MSG_HANDLER_CPP_SOURCES:.cpp=.o)


LIB_MSG_HANDLER_OBJS = $(addprefix $(LIB_DIR)/,$(MSG_HANDLER_OBJECTS))

IPC_SERVER_OBJECTS = $(IPC_SERVER_SOURCES:.c=.o)
LIB_IPC_SERVER_OBJS = $(addprefix $(LIB_DIR)/,$(IPC_SERVER_OBJECTS))

IPC_CLIENT_OBJECTS = $(IPC_CLIENT_SOURCES:.cpp=.o) $(IPC_CLIENT_GEN_SOURCES:.cc=.o)
LIB_IPC_CLIENT_OBJS = $(addprefix $(LIB_DIR)/,$(IPC_CLIENT_OBJECTS))

MsgHandler = $(LIB_DIR)/libmsghandler.a


IpcClient = $(LIB_DIR)/libIpcClient.a
IpcServer = $(LIB_DIR)/libIpcServer.a

.PHONY: clean

all: $(MsgHandler)  $(IpcClient) $(IpcServer)

$(MsgHandler): $(LIB_MSG_HANDLER_OBJS)
	$(AR) -r $@ $^

$(IpcClient): $(LIB_IPC_CLIENT_OBJS)
	$(AR) -r $@ $^

$(IpcServer): $(LIB_IPC_SERVER_OBJS)
	$(AR) -r $@ $^



$(LIB_DIR)/%.o: %.c
	@mkdir -p $(@D)
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(LIB_DIR)/%.o: %.cpp
	@mkdir -p $(@D)
	$(CXX) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(LIB_DIR)/%.o: %.cc
	@mkdir -p $(@D)
	$(CXX) $(CFLAGS) $(INCLUDES) -c $< -o $@

clean:
	$(RM) -rf $(LIB_DIR)
